package com.ce.pms.log4ce;

import java.util.List;
import java.util.Map;

import org.aspectj.lang.JoinPoint;

import com.ce.common.util.BlankUtil;
import com.ce.component.log4ce.aop.constructor.support.AbstractBizDescriptionHandler;
import com.ce.component.log4ce.aop.constructor.support.BizDescriptionHandler;
import com.ce.component.log4ce.aop.constructor.support.OriginalObjectHolder;
import com.ce.component.log4ce.aop.parser.BizLogParser;
import com.ce.pms.common.web.ICacheManage;
import com.ce.pms.customer.vo.CustomerHouseVO;
import com.ce.pms.customer.vo.CustomerVO;
import com.ce.pms.customer.vo.IndividualVO;
import com.ce.pms.customer.vo.OrganizationVO;

public class CustomerEditHandler extends AbstractBizDescriptionHandler implements BizDescriptionHandler {

	private Log4ceHelper log4ceHelper = Log4ceHelper.getInstance();


	/***
	 * 功能描述:  将客户对像转换为客户日志记录对象<br>
	 * @param vo
	 * @return
	 * Create author:王浩羽 <br>
	 * Create on:2008-11-8  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private CustomerRecord toRecord(CustomerVO vo){
		CustomerRecord record = null;
		if(CustomerVO.INDIVIDUAL_CODE.equals(vo.getParticipantTypeCode())){
			record = new CustomerRecord();
			//处理个人业主
			IndividualVO individualVO = (IndividualVO)vo;
			record.setOwnerName(individualVO.getParticipantName());
			record.setCertificateName(individualVO.getCertificateTypeName());
			record.setCertificateNo(individualVO.getCertificateNo());
			record.setTelephone(individualVO.getTelephone());
		}else if(CustomerVO.ORGNIZATION_CODE.equals(vo.getParticipantTypeCode())) {
			record = new CustomerRecord();
			//处理个组织业主
			record.setOrganization(true);
			OrganizationVO organizationVO = (OrganizationVO)vo;
			record.setOrgName(organizationVO.getParticipantName());
			record.setOrgAddress(organizationVO.getAddress());
			record.setOrgTelephone(organizationVO.getTelephone());
			record.setOrgContactPerson(organizationVO.getOrganizationLinkMan());
			record.setOrgZipcode(organizationVO.getZip());
		}
		return record;
	}



	public Map<String, Object> getNamedParametersMap(JoinPoint joinPoint, Object obj, BizLogParser logParser) {

		Map<String,Object> map = logParser.getLogArguments(joinPoint);
		List<CustomerHouseVO> houseList = (List<CustomerHouseVO>)map.get("houseList");
		CustomerVO source = (CustomerVO)OriginalObjectHolder.ORIGINAL_OBJECTS.get().get("sourceCustomerVO");
		CustomerVO target = (CustomerVO)OriginalObjectHolder.ORIGINAL_OBJECTS.get().get("targetCustomerVO");

		if(!BlankUtil.isBlank(source) && !BlankUtil.isBlank(target))
		{
			//初始记录
			CustomerRecord sourceRecord = toRecord(source);
			String srcHouseNumbers = (String) OriginalObjectHolder.ORIGINAL_OBJECTS.get().get("srcHouseNumbers");
			sourceRecord.setHouseNumber(srcHouseNumbers);

			//目标记录
			CustomerRecord targetRecord = toRecord(target);
			String desHouseNumbers = log4ceHelper.genHouseNumbers(log4ceHelper.getHousesFilterByStatus(houseList,ICacheManage.DELETE));
			targetRecord.setHouseNumber(desHouseNumbers);

			//展示信息
			String displayInfo = CustomerRecord.displayChangedInfo(sourceRecord, targetRecord);
			map.put("editingOperationInfo", displayInfo);
		}

		map.putAll(logParser.getLogStaticContents(joinPoint));
		return map;
	}
}
